﻿using System;
using System.Collections.Generic;
using System.Text;

namespace VS2048
{
    public struct GameSummaryInfo
    {
        public int TotalCellCount;
        public int CurrentCellCount;
        public int Score;
        public CellCoordinate NewCellLocation;
    }
    /// <summary>
    /// 当前cell的存储容器
    /// 相当于MVC里面的M
    /// </summary>
    internal class CellsHolder
    {
        private int _rowNum = 0;
        private int _colNum = 0;

        private ICell[,] _celltable = null;
        private ICellMergeStrategy _cms = null;

        public int RowNum
        {
            get { return _rowNum; }
            set { _rowNum = value; }
        }

        public int ColumnNum
        {
            get { return _colNum; }
            set { _colNum = value; }
        }

        internal ICellMergeStrategy CellMergeStrategy
        {
            get { return _cms; }
            set { _cms = value; }
        }
        /// <summary>
        /// Cell表，主要用于合并策略
        /// </summary>
        public ICell[,] Cells
        {
            get { return _celltable; }
        }

        public bool AddCell(CellCoordinate loc, ICell cell)
        {
            if (_celltable == null)
                _celltable = new ICell[_rowNum, _colNum];
            if (_celltable[loc.RowIndex, loc.ColIndex] == null)
                _celltable[loc.RowIndex, loc.ColIndex] = cell;
            return true;
        }
        /// <summary>
        /// 合并有效Cell,返回当前合并分值
        /// </summary>
        /// <param name="keyVal">上下左右方向键值</param>
        public int MergeNearCell(int keyVal)
        {
            int score = 0;
            if (_cms != null)
                score = _cms.Merge(keyVal, _celltable);
            return score;
        }
        /// <summary>
        /// 真正意义的满格是满格加上没有可以合并的
        /// </summary>
        /// <returns></returns>
        public bool IsFull()
        {
            int validnum = 0;
            if (_celltable == null) return false;
            for (int i = 0; i < _celltable.GetLength(0); i++)
            {
                for (int j = 0; j < _celltable.GetLength(1); j++)
                {
                    if (_celltable[i, j] != null) ++validnum;
                }
            }
            return validnum >= _rowNum * _colNum;
        }
        /// <summary>
        /// If Can Insert Cell
        /// </summary>
        /// <param name="rowidx"></param>
        /// <param name="colidx"></param>
        /// <returns></returns>
        public bool CheckPosition(int rowidx, int colidx)
        {
            bool canInsert = true;
            if (_celltable == null) return true;
            if (_celltable[rowidx, colidx] != null)
            {
                canInsert = false;
            }
            return canInsert;
        }

        public void ClearCell()
        {
            _celltable = null;
        }

        public int GetCurrCellCount()
        {
            int count = 0;
            if (Cells != null)
            {
                for (int i = 0; i < Cells.GetLength(0); i++)
                {
                    for (int j = 0; j < Cells.GetLength(1); j++)
                    {
                        if (Cells[i, j] != null)
                            ++count;
                    }
                }
            }
            return count;
        }
    }
}
